package pe.tp2.servlets;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import pe.tp2.beans.Tender;
import pe.tp2.dao.service.TenderDAOService;
import pe.tp2.dao.service.TodoBusinessDelegate;

/**
 * Servlet implementation class SRegistrarTender
 */
public class SRegistrarTender extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SRegistrarTender() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		procesarLlamada(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		procesarLlamada(request, response);
	}

	private void procesarLlamada(HttpServletRequest request,
			HttpServletResponse response) {
		String accion = request.getParameter("accion");
		String pagina = "";
		
		
		try {
			if(accion.equals("mostrarRegistro")){
				pagina = mostrarRegistro(request, response);
			}else if(accion.equals("mostrarAgregarEmbarque")){
				pagina = mostrarAgregarEmbarque(request, response);
			}else if(accion.equals("registrarTender")){
				pagina = registrarTender(request, response);
			}
			response.sendRedirect(pagina);
			
		} catch (Exception e) {
			e.printStackTrace();
		}		
	}

	private String registrarTender(HttpServletRequest request, HttpServletResponse response) {
		try {
			TenderDAOService tenderService = TodoBusinessDelegate.getTenderService();
			
			Tender objTender = new Tender();
			objTender.setMext(request.getParameter("mext"));
			objTender.setEstado("1");
			objTender.setTipo(request.getParameter("tipo"));
			objTender.setFechaRegistro(request.getParameter("fechaRegistro"));
			objTender.setFechaReceIni(request.getParameter("fechaReceIni"));
			objTender.setFechaReceFin(request.getParameter("fechaReceFin"));
			objTender.setFechaEvalIni(request.getParameter("fechaEvalIni"));
			objTender.setFechaEvalFin(request.getParameter("fechaEvalFin"));
			
			
			tenderService.registraTender(objTender);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "SRegistrarTender?accion=mostrarRegistro";
	}

	private String mostrarAgregarEmbarque(HttpServletRequest request, HttpServletResponse response) {
		
		Tender objTender = (Tender) request.getSession().getAttribute("objTender");
		
		if(!(objTender != null)){
			objTender = new Tender();	
		}
		
		objTender.setMext(request.getParameter("mext"));
		objTender.setEstado("1");
		objTender.setTipo(request.getParameter("tipo"));
		objTender.setFechaRegistro(request.getParameter("fechaRegistro"));
		objTender.setFechaReceIni(request.getParameter("fechaReceIni"));
		objTender.setFechaReceFin(request.getParameter("fechaReceFin"));
		objTender.setFechaEvalIni(request.getParameter("fechaEvalIni"));
		objTender.setFechaEvalFin(request.getParameter("fechaEvalFin"));
		
		request.getSession().setAttribute("objTender",objTender);
		
		return "pages/inc/inc_registrarEmbarque.jsp";
	}

	private String mostrarRegistro(HttpServletRequest request, HttpServletResponse response) {
		request.getSession().removeAttribute("lstEmbarques");
		request.getSession().removeAttribute("objEmbarque");
		request.getSession().removeAttribute("lstTender");
		try {
		
			TenderDAOService tenderService = TodoBusinessDelegate.getTenderService();
			ArrayList<Tender> lstTender = tenderService.buscarTender(null);
			
			System.out.println("cantidad: "+lstTender.size());
			
			request.getSession().setAttribute("lstTender", lstTender);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return "pages/registroTender.jsp";
	}

}
